package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.auth.LicenseModel;
import cn.com.yx.ms.domain.auth.ServerInfoModel;
import cn.com.yx.ms.domain.dbmodel.AuthClientServerInfo;
import cn.com.yx.ms.persistence.AuthClientLicenseMapper;
import cn.com.yx.ms.persistence.AuthClientServerInfoMapper;
import cn.com.yx.ms.service.IServerInfoService;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.security.Des3Kit;
import cn.yx.framework.system.SystemUtil;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.util.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * ApphubServiceImpl
 */
@Service
@Transactional
public class ServerInfoServiceImpl implements IServerInfoService {

    // private String filepath = BaseConstant.getUploadConfig().getFilepath() + "/serverinfo";

    @Resource
    private AuthClientServerInfoMapper authClientServerInfoMapper;
    @Resource
    private AuthClientLicenseMapper authClientLicenseMapper;

    @Override
    public void exportInfo(HttpServletResponse res) throws Exception {

        String filepath = BaseConstant.getUploadConfig().getFilepath() + "/export/serverinfo";
        //获取服务器信息
        String cpuid = SystemUtil.getCpuId();
        String[] ipMac = SystemUtil.getIpMac();

        //加密服务器信息
        String encryptData = Des3Kit.encrypt(cpuid + "|" + ipMac[1]);

        //生成服务器加密文件信息
        byte sendByte[] = encryptData.getBytes("UTF-8");//String转换为byte[]
        String fileName = "client-serverinfo-" + DateUtil.date2String(new Date(), "yyyyMMddHHmmss") + ".ysb";
        FileUtil.writeFile(filepath,
                fileName,
                sendByte);

        //导出
        HttpExportFile(res, filepath, fileName);

    }

    private void HttpExportFile(HttpServletResponse res, String path, String fileName) throws Exception {
        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;

        os = res.getOutputStream();
        bis = new BufferedInputStream(new FileInputStream(new File(path + "/" + fileName)));
        int i = bis.read(buff);
        while (i != -1) {
            os.write(buff, 0, buff.length);
            os.flush();
            i = bis.read(buff);
        }

        if (bis != null) {
            bis.close();
        }

    }

    @Override
    public ServerInfoModel findThisServer() throws Exception {
        ServerInfoModel serverInfoModel = null;
        AuthClientServerInfo authClientServerInfo = null;
        int count = authClientServerInfoMapper.isExistServerInfo();
        if (count > 0) {
            serverInfoModel = authClientServerInfoMapper.findServerInfoModel();
            authClientServerInfo = authClientServerInfoMapper.selectByPrimaryKey(serverInfoModel.getGuid());
            authClientServerInfo.setCpuid(SystemUtil.getCpuId());
            authClientServerInfo.setMac(SystemUtil.getIpMac()[1]);
            authClientServerInfoMapper.updateByPrimaryKey(authClientServerInfo);
        } else {
            authClientServerInfo = new AuthClientServerInfo();
            authClientServerInfo.setGuid(UUID.randomUUID().toString());
            authClientServerInfo.setCpuid(SystemUtil.getCpuId());
            authClientServerInfo.setMac(SystemUtil.getIpMac()[1]);
            authClientServerInfo.setCreatetime(new Date());
            authClientServerInfo.setIsdeleted(0);
            authClientServerInfoMapper.insert(authClientServerInfo);
        }

        if (serverInfoModel == null) {
            serverInfoModel = authClientServerInfoMapper.findServerInfoModel();
        }
        //License使用情况赋值
        serverInfoModel.setLicenseUseConditionModels(authClientServerInfoMapper.findLicUseConditionByServerId());
        //设备激活情况赋值
        serverInfoModel.setDevActiveModels(authClientServerInfoMapper.findDevActiveByServerId());
        return serverInfoModel;
    }

    @Override
    public List<LicenseModel> findLicenseModel(ApiQueryBase query) {
        return authClientLicenseMapper.findLicenseModel(query);
    }


}